//
//  HTStatusOriginalVIew.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/21.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
import SnapKit
class HTStatusOriginalVIew: UIView {
 //记录原创微博约束
    var originalViewBottomConstraint:Constraint?
    
    //定义属性赋值用
    var status:HTStatus?{
        
        didSet{
            
            
            
            userImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            //昵称
            nameLabel.text = status?.user?.screen_name
            //会员等级
//            status?.user?.mbrank
            GradeVip.image = getGradeVipImage(status?.user?.mbrank ?? 0)
            //时间
            timeLabel.text = "时间"
            
            //来自
            comefromLabel.attributedText = getSoureSting(status?.source)
            
            //认证
            authenticationImageView.image = getauthenticationImage(status?.user?.verified_type ?? -1)
            //微博正文
            contentLabel.text = status?.text
            //卸载原来的约束
            self.originalViewBottomConstraint?.uninstall()
            
            if status?.pic_urls?.count != 0 {
                //给配图赋值
                pictureView.pictureInfo = status?.pic_urls
                //有图  原创微博的底部  - 配图底部+10  显示
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                    
                })
                pictureView.hidden = false
                
            }else{
                //没图 原创微博底部 = 微博正文底部+10  、、隐藏
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                    
                })
                pictureView.hidden = true
            }
        }
        
    }
    //处理微博时间=====================
    
    
    //处理微博来源字符串
    func getSoureSting(source:String?)->NSMutableAttributedString{
        
        guard let s = source where s.containsString("\">") else{
            
            return NSMutableAttributedString(string: "")
        }
        //开始截取范围
        let startRange = s.rangeOfString("\">")!
        //结束
        let endRange = s.rangeOfString("</a")!
        
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        //拼接结果
        let name = "来自\(result)"
        
        //富文本
        let  attr = NSMutableAttributedString(string: name)
        
        let range = (name as NSString).rangeOfString("来自")
        
        attr.addAttribute (NSForegroundColorAttributeName, value: UIColor.grayColor(),range: range)
        
        return attr
    }
    //会员等级 0 - 6
    func getGradeVipImage(Grade: Int) -> UIImage?{
        if Grade > 0 && Grade < 7{
        
            return UIImage(named: "common_icon_membership_level\(Grade)")
        }else{
            
         return UIImage(named: "common_icon_membership_expired")
        }
    }
    
    //认证图片 /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    func getauthenticationImage(authentication:Int) ->UIImage?{
        switch authentication{
        case 0:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220 :
            return UIImage(named: "avatar_grassroot")
        default :
            return nil
        }
        
        
    }
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //设置视图
    func setupUI(){
        //添加控件设置约束
        
        addSubview(userImageView)
        addSubview(nameLabel)
        addSubview(GradeVip)
        addSubview(timeLabel)
        addSubview(comefromLabel)
        addSubview(authenticationImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        userImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSizeMake(35, 35))
            
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(10)
            make.leading.equalTo(userImageView.snp_trailing).offset(10)
            
        }
        
        GradeVip.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
      
        }
      
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(userImageView)
        }
        comefromLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
            
        }
        authenticationImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(10)
            make.leading.equalTo(userImageView)
            make.width.equalTo(SCREENW-20)
        }
        
       
        //图
        pictureView.snp_makeConstraints { (make) -> Void in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        //原创微博的约束
        self.snp_makeConstraints { (make) -> Void in
           self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载控件
    //头像
    lazy var userImageView :UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    lazy var nameLabel : UILabel = {
        
        let namelabel = UILabel()
        
        namelabel.textColor = UIColor.darkGrayColor()
        
        namelabel.font = UIFont.systemFontOfSize(16)
  
        return namelabel
        
    }()
    
    //会员等级
    lazy var GradeVip:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    
    //时间
    lazy var timeLabel:UILabel = {
       let timelabel = UILabel()
        
        timelabel.textColor = UIColor.orangeColor()
        
        timelabel.font = UIFont.systemFontOfSize(12)
        
        return timelabel
    }()
    //来自
    lazy var comefromLabel:UILabel = {
        let comeFromLabel = UILabel()
        
        comeFromLabel.textColor = UIColor.orangeColor()
        
        comeFromLabel.font = UIFont.systemFontOfSize(12)
        return comeFromLabel
        }()
    
    //认证
    lazy var authenticationImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vgirl"))
    
    //微博正文
    lazy var contentLabel :UILabel = {
       let contentlabel = UILabel()
        contentlabel.textColor = UIColor.blackColor()
        contentlabel.font = UIFont.systemFontOfSize(16)
        contentlabel.numberOfLines = 0
        return contentlabel
        
    }()
    
    //微博配图
    lazy var pictureView:HTStatusPictureView = HTStatusPictureView()
    
}
